package com.miao.service.impl;

import com.miao.dao.UserDao;
import com.miao.entity.User;
import com.miao.service.UserService;
import com.miao.util.BeanCopyUtil;
import com.miao.vo.RegisterVo;
import com.miao.vo.ResultVo;
import com.miao.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserSerivceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public ResultVo getNameId(Integer userId) {
        User user = userDao.selectById(userId);
        UserVo userVo = new UserVo();
        BeanCopyUtil.copyProperties(user, userVo);
        return ResultVo.success(userVo);
    }

    @Override
    public ResultVo outNameId(Integer userId) {
        User user = new User();
        user.setUserId(userId);
        user.setUserStatus(0);
        int i= userDao.reviseId(user);
        return ResultVo.success(i);
    }

    @Override
    public ResultVo logon(RegisterVo registerVo) {
        User user = new User();
        BeanCopyUtil.copyProperties(registerVo, user);
        Integer integer = userDao.insert(user);
        return ResultVo.success(integer);
    }

    @Override
    public ResultVo register(RegisterVo registerVo) {
        User user = new User();
        BeanCopyUtil.copyProperties(registerVo, user);
        User selectByOpenId = userDao.selectByOpenId(user.getOpenId());
        if (selectByOpenId != null) {
            return ResultVo.success(selectByOpenId);
        }
        user.setCreateTime(new Date().getTime());
        user.setUpdateTime(new Date().getTime());
        Integer integer = userDao.insert(user);
        return ResultVo.success(integer);
    }
}
